
var _FILENAME = "APPSTART";
cc.Class({
    extends: cc.Component,

    properties: {

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        require("MsgTypes");
        cc.log = require("InternalLog").log;
        cc.log("onLoad");
    },

    start () {
        cc.log("start");
        cc.net = require("Net");  
        this.initNetWork();
        this.initHandlers();
    },

    onDestroy () { },

    // update (dt) {},

    initHandlers () {
        cc._s.addHandler(cc.SYS_MSG.CONNECT, function (msg, s) {
            cc.log("CONNECT " + msg);
            var _msg = { 
                acc: "test",
                pwd: "123456",
                age: 18
            };
            cc.log(_msg);
            s.send(cc.MSG.LOGIN_REQ, _msg);
        }, _FILENAME);

        cc._s.addHandler(cc.SYS_MSG.RECONNECT, function (msg, s) {
            cc.log("RECONNECT " + _msg);
        }, _FILENAME);

        cc._s.addHandler(cc.MSG.LOGIN_RES, function (msg, s) {
            cc.log("LOGIN_RES " + JSON.stringify(msg));
        }, _FILENAME);

        cc._s.addHandler(cc.MSG.ERROR, function (msg, s) {
            cc.log("ERROR " + msg);
        }, _FILENAME);
    },

    initNetWork () {
        cc._s = cc.net.createNetClient("ws://192.168.1.80:10086");
        cc.net.loadProto(function (protoroot) {
            cc.protoRoot = protoroot;
            cc._s.connect();
        });
    },

});
